package binary;

public class Demo06 {
    public static void main(String[] args) {
        /*
         *掩码计算
         */
        //int n = 0xfffffffc;//n=-3
        //int n = 0xffffffff;//n=-1

        //int n = Integer.MAX_VALUE;
        //System.out.println(Integer.toBinaryString(n));

        //int n = Integer.MIN_VALUE;
        //System.out.println(n);
        int n = 0x179d5d9e;//00010111 10011101 01011101 10011110
        //4为掩码：0xf  0b1111  15
        //6位掩码：0x3f 0b111111  63
        //8位掩码：0xff 0b11111111  255
        int m = 0xff;      //00000000 00000000 00000000 11111111
/*        int k = n & m;
        System.out.println(Integer.toBinaryString(n));
        System.out.println(Integer.toBinaryString(m));
        System.out.println(Integer.toBinaryString(k));*/

        int b1 = (n >>> 24) & m;
        int b2 = (n >>> 16) & m;
        int b3 = (n >>> 8) & m;
        int b4 = n & m;
        System.out.println(Integer.toBinaryString(b1));
        System.out.println(Integer.toBinaryString(b2));
        System.out.println(Integer.toBinaryString(b3));
        System.out.println(Integer.toBinaryString(b4));


        int g = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;//错位运算
        System.out.println(g == n);

    }
}
